'use strict';

const path = require('path');
var webpack = require('webpack');
const AutoDllPlugin = require('autodll-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin'); //生成html
const ip = require("ip").address();

let conf = {
    devtool: 'source-map',
    entry: {
        index: "./src/entry/index.jsx" //唯一入口文件
    },
    output: {
        path: path.resolve(__dirname, './dist'), //打包后的文件存放的地方
        filename: '[name].[chunkHash:8].js', //打包后输出文件的文件名
        chunkFilename: "[name].[chunkHash:8].js"
    },
    cache: true,
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            }, {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'postcss-loader'
                ]
            }, {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'postcss-loader',
                    'sass-loader'
                ]
            }, {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'postcss-loader',
                    'less-loader'
                ]
            }, {
                test: /\.(png|jpg|jpeg|gif)$/,
                loader: 'url-loader?name=./images/[hash:8].[ext]&limit=8192'
            }, {
                //html模板加载器，可以处理引用的静态资源，默认配置参数attrs=img:src，处理图片的src引用的资源
                //比如你配置，attrs=img:src img:data-src就可以一并处理data-src引用的资源了，就像下面这样
                test: /\.html$/,
                loader: "html-loader?attrs=img:src img:data-src"
            }, {
                //文件加载器，处理文件静态资源
                test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file-loader?name=./fonts/[name].[ext]'
            }
        ]
    },

    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': '"development"'
            }
        }),
        new webpack.NamedModulesPlugin(),
        new webpack.ProvidePlugin({
            $: 'jquery',
            'jQuery': 'jquery',
            'windows.jQuery': 'jquery'
        }),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['vendor', 'manifest'],
            minChunks: Infinity
        }),
        new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML
            filename: './index.html', //生成的html存放路径，相对于path
            template: './src/entry/index.html.ejs', //html模板路径
            favicon: './src/entry/favicon.ico',
            inject: true, //js插入的位置，true/'head'/'body'/false
            hash: false, //为静态资源生成hash值
            minify: { //压缩HTML文件
                removeComments: true, //移除HTML中的注释
                collapseWhitespace: false //删除空白符与换行符
            },
            chunksSortMode: 'dependency'
        }),
        new AutoDllPlugin({
            inject: true, // will inject the DLL bundle to index.html
            debug: true,
            filename: '[name].[hash:8].js',
            path: './dll',
            entry: {
                vendor: ['react', 'react-dom', 'react-router-dom', 'antd']
            }
        })
    ],
    resolve: {
        modules: ['node_modules', './node_modules'],
        extensions: ['.js', '.jsx'],
        alias: {
            "component": path.resolve("src/component"),
            "container": path.resolve("src/container"),
            "layouts": path.resolve("src/layouts"),
            "action": path.resolve("src/action"),
            "asset": path.resolve("src/asset"),
            "util": path.resolve("src/util"),
            "page": path.resolve("src/page")
        }
    },
    devServer: {
        historyApiFallback: true,
        inline: true,
        port: 9090,
        host: ip,
        compress: true,
        stats: {
            chunkModules: false,
            modules: false,
            error: true,
            errorDetails: true,
            publicPath: true
        },
        proxy: {
            '/api': {
                target: 'http://localhost:8080',
                // target: 'http://www.cfun-world.cn',
                pathRewrite: {
                    '^/api': '/api'
                },
                changeOrigin: true
            }
        }
    }
};

module.exports = conf;
